package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.constant.BspsAppParamConstant;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.GenerateLoginOrderRequest;
import cc.rengu.igas.bsps.facade.response.GenerateLoginOrderResponse;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.SysParamMapper;
import cc.rengu.oltp.service.common.dao.UserOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.SysParamMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.UserOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.service.common.enums.OrderStatusEnum;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.OrderService;
import cc.rengu.oltp.service.realize.impl.OrderServiceImpl;
import cc.rengu.oltp.utility.util.*;

/**
 * Created by zhangxuran on 2020/04/14
 */
public class GenerateLoginOrderService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        GenerateLoginOrderRequest generateLoginOrderRequest = new GenerateLoginOrderRequest();
        ConvertUtil.convertOutput(generateLoginOrderRequest);
        return generateLoginOrderRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        GenerateLoginOrderRequest generateLoginOrderRequest = (GenerateLoginOrderRequest) request;
        /*获取新题请求参数:法人行号，订单参数，订单过期时间*/
        String instId = generateLoginOrderRequest.getHeader().getInstId();
        String loginOrderId = generateLoginOrderRequest.getLoginOrderId();
        String orderExpiryTime = generateLoginOrderRequest.getOrderExpiryTime();
        if (StringUtil.isEmptyOrNull(loginOrderId)) {
            loginOrderId = RandomUtil.getUUID();
        }
        /*默认登录订单一分钟超时*/
        if (StringUtil.isEmptyOrNull(orderExpiryTime)) {
            orderExpiryTime = "60";
        }
        /*根据法人行查询参数法人行域名*/
        SysParamMapper sysParamMapper = new SysParamMapperImpl();
        SysParam sysParam = sysParamMapper.selectSysParamByPrimaryKey(instId, BspsAppParamConstant.MCHNT_PARAM_TYPE, "loginCodeUrl");
        if (null == sysParam) {
            rglog.error("查询行域名信息失败！");
            throw new BizException(RespCodeEnum.QUERY_NULL.getRespCode(), RespCodeEnum.QUERY_NULL.getRespDesc());
        }
        String bankDomain = sysParam.getParamValue();
        /*生成用户登录订单*/
        UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
        UserOrderInfo userOrderInfo = new UserOrderInfo();
        /*法人行赋值*/
        userOrderInfo.setInstId(instId);
        /*交易日期赋值*/
        userOrderInfo.setTxnDate(generateLoginOrderRequest.getHeader().getTransDate());
        /*交易时间赋值*/
        userOrderInfo.setTxnTime(DateUtil.getCurrentTime());
        /*内部交易码赋值*/
        userOrderInfo.setTxnNum(generateLoginOrderRequest.getHeader().getTxnNum());
        /*业务细分赋值*/
        userOrderInfo.setBizType(generateLoginOrderRequest.getHeader().getBizType());
        /*源系统标识赋值*/
        userOrderInfo.setMsgSrcId(generateLoginOrderRequest.getHeader().getSrcSysId());
        /*交易渠道标识赋值*/
        userOrderInfo.setTransChannelId(generateLoginOrderRequest.getHeader().getChanlId());
        /*用户订单号赋值*/
        OrderService orderService = new OrderServiceImpl();
        userOrderInfo.setUserOrderId(orderService.generateUserOrderId());
        /*登录订单号赋值*/
        userOrderInfo.setMchntOrderId(loginOrderId);
        /*登录订单url赋值*/
        userOrderInfo.setOrderUrl(bankDomain.replace("orderUrl", "orderUrl=" + xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM)));
        /*前端系统流水号*/
        userOrderInfo.setFrontSeqNum(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.FRONT_SEQ_NUM));
        /*系统流水号赋值*/
        userOrderInfo.setSysSeqNum(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
        /*订单创建时间赋值*/
        userOrderInfo.setOrderCreateTime(DateUtil.getCurrentDateTime("yyyyMMddHHmmss"));
        /*订单超时时间赋值*/
        userOrderInfo.setOrderExpiryTime(DateUtil.getPrevioustimes(Long.parseLong(orderExpiryTime) * (-1000), "yyyyMMddHHmmss"));
        /*订单状态赋值*/
        userOrderInfo.setOrderStatus(OrderStatusEnum.INIT.getStatus());
        /*订单交易状态赋值*/
        userOrderInfo.setTransStatus(TransStatusEnum.TRANS_INIT.getStatus());

        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();
        userOrderInfo.setCreateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iRtnCode = userOrderInfoMapper.insertUserOrderInfo(userOrderInfo);
        if (0 != iRtnCode) {
            dbsUtil.dbsEndTransaction(false);
            rglog.error("生成订单二维码失败！");
            throw new BizException(RespCodeEnum.GENERATE_QRCODE_ERROR.getRespCode(), RespCodeEnum.GENERATE_QRCODE_ERROR.getRespDesc());
        }
        dbsUtil.dbsEndTransaction(true);
        /*返回报文赋值*/
        GenerateLoginOrderResponse generateLoginOrderResponse = new GenerateLoginOrderResponse();
        /*登录订单号赋值*/
        generateLoginOrderResponse.setLoginOrderId(loginOrderId);
        /*登录URL赋值*/
        generateLoginOrderResponse.setLoginUrl(userOrderInfo.getOrderUrl());
        /*交易流水号赋值*/
        generateLoginOrderResponse.setSysSeqNum(userOrderInfo.getSysSeqNum());
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse<GenerateLoginOrderResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(generateLoginOrderResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        GenerateLoginOrderResponse generateLoginOrderResponse = (GenerateLoginOrderResponse) bizResponse.getResult();
        ConvertUtil.convertInput(generateLoginOrderResponse);
    }
}
